#include "cylinder.h"

using namespace PBVP;

bool Cylinder::initialized = false;
GLSLProgram* Cylinder::pGLSLProg = NULL;
GLUquadricObj* Cylinder::quadObj = NULL;

void Cylinder::render()
{
	if(pGLSLProg && pGLSLProg->isValid()) {
		// render using shaders

		return;
	}
	// render using standard OpenGL
	glPushMatrix();
	glTranslatef(start.x, start.y, start.z);
	glRotatef(angle, axis.x, axis.y, axis.z);
	gluCylinder(quadObj, radius, radius, (end-start).length(), 12, 6);
	glPopMatrix();
}

Cylinder* Cylinder::makeCylinder(const Vector3 &s, const Vector3 &e, double r)
{
	if(!initialized) {
		initGLSLProgram();
	}
	Vector3 z(0, 0, 1.0f);
	Vector3 dir = e - s;

	Cylinder *cyl = new Cylinder();
	cyl->start = s;
	cyl->end = e;
	cyl->radius = r;
	cyl->angle = z.angleBetween(dir);
	cyl->axis = z.crossProduct(dir);
	cyl->axis.normalize();

	return cyl;
}

void Cylinder::initGLSLProgram()
{
	initialized = true;

	quadObj = gluNewQuadric();

	// TODO
}
